package com.business.action.work;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.business.bean.basic.ProjectBean;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.util.WebUtils;

/**
 * 项目管理
 * @author fengjian
 *
 */
@Controller
@RequestMapping("/roject")
public class ProjectAction {

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/page.do")
	public PageBean pageProject(HttpServletRequest request, Service service) {
		PageBean res = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(ProjectBean.class);
		query.setTableName("WORK_PROJECT_VIEW");
		query.createSql(WebUtils.getRequestData(request));
		res.setData(dao.list());
		return res;
	}

	/**
	 * 用户下拉列表查询获取可用的项目列表
	 * 优先列出用户分配到的项目
	 */
	@ResponseBody
	@RequestMapping("/list.do")
	public PageBean listProject(HttpServletRequest request, Service service) {
		Map<String, Object> map = WebUtils.getRequestData(request);
		PageBean res = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(ProjectBean.class);
		query.createSql(map);
		List<ProjectBean> list = dao.list();
		Object userID = map.get("userID");
		if (userID != null) {
			// 根据用户所在组织查找所有的项目
			/* query.setEntityClass(ProjectUserBean.class);
			query.setSQL("select t1.userID,t2.projectID ");
			query.addSql("from WORK_PROJECT_USER t1 left join WORK_PROJECT_ORG t2 on t1.orgID=t2.orgID ");
			query.addSql("where t1.userID=?");
			query.addParameter(userID);
			List<ProjectUserBean> l = dao.list();
			if (l.size() > 0) {
				Map<String, ProjectBean> m = new HashMap<>();
				for (ProjectBean p : list) {
					m.put(p.getProjectID(), p);
				}
				// 如果找到，把属于个人的项目顺序放置在前面，其它放置在后面
				List<ProjectBean> ls = new ArrayList<>();
				for (ProjectUserBean p : l) {
					ProjectBean pb = m.remove(p.getProjectID());
					if (pb != null) {
						ls.add(pb);
					}
				}
				ls.addAll(m.values());
				list = ls; 
			}*/
		}
		res.setData(list);
		return res;
	}
	
	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/save.do")
	public PageBean saveProject(HttpServletRequest request, Service service, ProjectBean bean) {
		PageBean res = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(ProjectBean.class);
		query.createSaveSql(bean);
		dao.update();
		return res;
	}
	
	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/delete.do")
	public PageBean deleteProject(HttpServletRequest request, Service service, String projectID) {
		PageBean res = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.createDeleteSql(ProjectBean.class, projectID);
		dao.update();
		return res;
	}
}
